R을 이용한 웹페이지

나는 누구일까…?

정준혁

내 이름은 무슨무슨 뜻이다. 나는 내 이름을 좋아한다. ### 사회학과 사회학과는 “사회”를 배우는 학과이다.

좋아하는 것들

  • 친구
    • 양승우
    • 김준수
  • 애착 물건
    • 아이폰xs
    • 에어팟
  • 음식
    • 소곱창
    • 삼겹살

일상 사진

#자료분석

##gglot2의 mpg데이터

library(ggplot2)

우리는 자동차 경비 데이터인 mpg데이터를 이용하여 배기량과 도시연비의 관계를 그래프로 살펴보겠다.

먼저 mpg 데이터를 살펴보겠습니다.

head(mpg,10)
## # A tibble: 10 x 11
##    manufacturer model displ  year   cyl trans drv     cty   hwy fl    class
##    <chr>        <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
##  1 audi         a4      1.8  1999     4 auto~ f        18    29 p     comp~
##  2 audi         a4      1.8  1999     4 manu~ f        21    29 p     comp~
##  3 audi         a4      2    2008     4 manu~ f        20    31 p     comp~
##  4 audi         a4      2    2008     4 auto~ f        21    30 p     comp~
##  5 audi         a4      2.8  1999     6 auto~ f        16    26 p     comp~
##  6 audi         a4      2.8  1999     6 manu~ f        18    26 p     comp~
##  7 audi         a4      3.1  2008     6 auto~ f        18    27 p     comp~
##  8 audi         a4 q~   1.8  1999     4 manu~ 4        18    26 p     comp~
##  9 audi         a4 q~   1.8  1999     4 auto~ 4        16    25 p     comp~
## 10 audi         a4 q~   2    2008     4 manu~ 4        20    28 p     comp~

산포도를 그려서 두 변수의 관계를 살펴보겠습니다.

ggplot(mpg, aes(x=displ, y=cty))+geom_point(aes(color=drv, size=class))
## Warning: Using size for a discrete variable is not advised.

자료 분석 1

필요한 라이브러리 불러오기

실행 때 나오는 메시지를 보여주지 않는다.

library(ggplot2)
library(dplyr)

자동차 연비 데이터인 mpg 데이터를 살펴보자.

head(mpg)
## # A tibble: 6 x 11
##   manufacturer model displ  year   cyl trans  drv     cty   hwy fl    class
##   <chr>        <chr> <dbl> <int> <int> <chr>  <chr> <int> <int> <chr> <chr>
## 1 audi         a4      1.8  1999     4 auto(~ f        18    29 p     comp~
## 2 audi         a4      1.8  1999     4 manua~ f        21    29 p     comp~
## 3 audi         a4      2    2008     4 manua~ f        20    31 p     comp~
## 4 audi         a4      2    2008     4 auto(~ f        21    30 p     comp~
## 5 audi         a4      2.8  1999     6 auto(~ f        16    26 p     comp~
## 6 audi         a4      2.8  1999     6 manua~ f        18    26 p     comp~
str(mpg)
## Classes 'tbl_df', 'tbl' and 'data.frame':    234 obs. of  11 variables:
##  $ manufacturer: chr  "audi" "audi" "audi" "audi" ...
##  $ model       : chr  "a4" "a4" "a4" "a4" ...
##  $ displ       : num  1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
##  $ year        : int  1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
##  $ cyl         : int  4 4 4 4 6 6 6 4 4 4 ...
##  $ trans       : chr  "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
##  $ drv         : chr  "f" "f" "f" "f" ...
##  $ cty         : int  18 21 20 21 16 18 18 18 16 20 ...
##  $ hwy         : int  29 29 31 30 26 26 27 26 25 28 ...
##  $ fl          : chr  "p" "p" "p" "p" ...
##  $ class       : chr  "compact" "compact" "compact" "compact" ...

mpg 데이터에서 자동차의 배기량(displ)과 도시연비(cty)의 관계를 그래프로 살펴보자

ggplot(mpg, aes(x=displ, y=hwy)) + 
  geom_point(aes(color=class, size=displ)) +
  stat_smooth(method = 'loess')

자료 분석 2

강원도 도시대기측정결과를 지도에 표현해보자.

지도 관련 패키지 설치 및 불러오기

  • 공간 지도 분석을 위한 패키지
  • maps: 세계 지도 데이터베이스
  • mapproj: 지도 상에 위도와 경도를 표시
  • ggplot2: map_data()를 이용하여 지도정보를 R로 불러오기
  • ggiraphExtra : 단계 구분도 표시
library(maps)
library(mapproj)
library(ggplot2)
library(ggiraphExtra)

`